package com.stottlerhenke.simbionic.editor;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import com.stottlerhenke.simbionic.editor.gui.SB_ProjectBar;

abstract public class SB_Function extends UserObject
{
    public static final Class SB_ActionClass=com.stottlerhenke.simbionic.editor.SB_Action.class;
    public static final Class SB_PredicateClass=com.stottlerhenke.simbionic.editor.SB_Predicate.class;
    public static final Class SB_BehaviorClass=com.stottlerhenke.simbionic.editor.SB_Behavior.class;
    
    private static final long serialVersionUID = 2302585093L + 1;

    protected  String _description = "";
    protected boolean _core = false;
    protected int _id = -1;
    protected  static Class[] _classes = null;

    public SB_Function()  // constructor for Externalizable object
    {
    }

    public SB_Function(String name)
    {
        super(name);
    }

    public boolean isCore(){return _core;}
    public void setCore(boolean core){_core=core;}
    
    public boolean isCellEditable()
    {
        return !_core || SimBionicEditor.DEV;
    }

    public String getToolTipText()
    {
        if (_description.length() > 0)
            return _description;
        else
            return null;
    }

    protected Class[] getRelatedClasses()
    {
        if (_classes == null)
        {
            _classes = new Class[3];
           
                _classes[0] = SB_ActionClass;
                _classes[1] = SB_PredicateClass;
                _classes[2] = SB_BehaviorClass;
           
        }
        return _classes;
    }

    public boolean shouldSort() { return true; }

    public String getWildName(int n)
    {
        String wild = "";
        for (int i = 0; i < n; ++i)
        {
            if (i < n - 1)
                wild += "*,";
            else
                wild += "*";
        }
        return _name + "(" + wild + ")";
    }

    public void compileSIM(BufferedWriter out)
    {
      try
      {
        // publish function name and ID
        out.write("\t" + SB_ProjectBar.getPString(_name) + " " + _id);
        out.newLine();
      }
      catch (IOException exception)
      {
        System.err.println("i/o exception");
      }
    }

    public void writeExternal(ObjectOutput out) throws IOException
    {
      super.writeExternal(out);

      out.writeObject(_description);
      out.writeBoolean(_core);
      out.writeInt(_id);
    }

    public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException
    {
      super.readExternal(in);

      _description = (String) in.readObject();
      _core = in.readBoolean();
      _id = in.readInt();
    }

    public abstract String getTag();

    public void exportXML(BufferedWriter out, int n) throws IOException
    {
      SB_ProjectBar.writeTabs(out, n);
      out.write("<name>" + _name + "</name>");
      out.newLine();
      SB_ProjectBar.writeTabs(out, n);
      out.write("<id>" + _id + "</id>");
      out.newLine();
      SB_ProjectBar.writeTabs(out, n);
      
      String escaped = _description.replaceAll("&", "&amp;");
      escaped = escaped.replaceAll("<", "&lt;");
      out.write("<descr>" + escaped + "</descr>");
      out.newLine();
      
      SB_ProjectBar.writeTabs(out, n);
      out.write("<core>" + (_core ? 1 : 0) + "</core>");
      out.newLine();
   }
	/**
	 * @return Returns the id.
	 */
	public int getId() {
		return _id;
	}
	/**
	 * @param id The id to set.
	 */
	public void setId(int id) {
		_id = id;
	}
	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return _description;
	}
	/**
	 * @param description The description to set.
	 */
	public void setDescription(String description) {
		_description = description;
	}
}
